package system.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;

import system.base.Customer;
import system.base.ProductCategory;
import system.base.Warehouse;
import system.base.WarehouseCategory;
import system.base.WarehouseCategoryRelation;

import com.apep.jwcs.service.AirSortReportExcel;
import com.apep.jwcs.service.AirSortReportService;
import com.hitrust.gs.bean.GSProductApepBean;
import com.hitrust.gs.bean.GSProviderBean;
import com.hitrust.gs.dao.GSMonthInfoDao;
import com.hitrust.gs.dao.GSProviderDAO;
import com.hitrust.gs.lib.GSAutoAuditongDateConfigurator;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.util.ResponseUtils;
import com.hitrust.gs.util.exception.GSException;

import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.DBEngine;
import framework.core.DBResult;
import framework.core.DateUtil;
import framework.core.LogWriter;
import framework.core.ObjectList;
import framework.core.SrvContext;
import framework.core.StringUtil;
import framework.core.UniversalRow;
import framework.core.UniversalTable;

public class ReportServletZTAll extends HttpServlet {
	private static final long serialVersionUID = 1;// 仅为不让本类出现警告 unnecessary
	private static final String IS_REPORT_PROCESSING = "isReportProcessing";
	public static final String REGEXP_SHORT_DATE = "yyyy-MM-dd";
	public void init(ServletConfig aConfig) throws ServletException {
		super.init(aConfig);
	}

	/** 开始执行报表 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		if (!isProcessContinue(request, response)) {
			return;
		}

		HttpSession session = request.getSession();
		session.setAttribute(IS_REPORT_PROCESSING, Boolean.TRUE);

		System.out.println("**ReportServlet start**");
		/** 时间拼接 开始************************************************** */
		String tDateString = new DateUtil().getFullString();
		String tYear = tDateString.substring(0, 4);
		String tMonth = tDateString.substring(5, 7);
		String tDate = tDateString.substring(8, 10);
		String tHour = tDateString.substring(11, 13);
		String tMin = tDateString.substring(14, 16);
		String tSec = tDateString.substring(17, 19);
		String fileName = "GW_REPORT_" + tYear + "_" + tMonth + "_" + tDate
				+ "_" + tHour + "_" + tMin + "_" + tSec;
		/** 时间拼接 结束************************************************** */

		/** 基础变量设置 开始************************************************* */
		String providerName = ""; // 供应商名称
		String inCustomerName = ""; // 供货单位名称
		String customerCode = ""; // 领料单位编码
		// String warehouseProduct = ""; //报表分类 此变量 使用地不明确 暂注释
		String getCustomers = "";// 领料单位
		String title = request.getParameter("title");
		String warehouseCode = ""; // 仓库编码
		String start = ""; // 查询起始时间
		String end = ""; // 查询终止时间
		String needAll = "";// 查询所有的纪录
		/** 基础变量设置 结束************************************************* */

		/**
		 * 1：安全库存报表
		 */
		/** 压库产品进销存报表 对供应商参数进行判断 开始 */
		if ("1".equals(title)) {
			if (!"".equals(request.getParameter("providerName"))
					&& request.getParameter("providerName") != null) {
				try {
					providerName = request.getParameter("providerName");
					providerName = SrvContext.fixString(providerName);
				} catch (Exception e) {
					providerName = "";
				}
			} else {
				
				providerName = "";
			}
			needAll = request.getParameter("needAll");
		}

		if (!"".equals(request.getParameter("warehouseCode"))
				&& request.getParameter("warehouseCode") != null) {
			warehouseCode = request.getParameter("warehouseCode");
		}
		String querytype = request.getParameter("querytype");
		String month = "";
		/** 压库产品进销存报表 对查询时间段进行判断 开始 */
		int sYear = 0;
		int sMonth = 0;
		if ("1".equals(querytype)) {
			String yearMonth = request.getParameter("begindate") + "-"
					+ request.getParameter("enddate");
			sYear = Integer.parseInt(request.getParameter("begindate").trim());
			sMonth = Integer.parseInt(request.getParameter("enddate").trim());
			int sDate = 31; // 每月的默认天数
			try {
				sDate = DateUtil.getMaxDayOfMonth(sYear, sMonth);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
			start = yearMonth + "-01";
			end = yearMonth + "-" + String.valueOf(sDate);
			if (String.valueOf(sMonth - 1).length() < 2) {
				month = String.valueOf(sYear) + "0"
						+ String.valueOf(sMonth - 1);
			} else {
				month = String.valueOf(sYear + sMonth);
			}
		} else {
			start = request.getParameter("begindate");
			if ("".equals(request.getParameter("enddate"))) {
				end = new framework.core.DateUtil().getDateString();
			} else {
				end = request.getParameter("enddate");
			}
			int tmpMonth = Integer.parseInt(start.substring(6, 7)) - 1;

			if (String.valueOf(tmpMonth).length() < 2) {
				month = start.substring(0, 4);
				month += "0" + String.valueOf(tmpMonth);
			} else {
				month = start.substring(0, 4);
				month += String.valueOf(tmpMonth);
			}
		}

		/** 压库产品进销存报表 对查询时间段进行判断 结束 */
		String before_end = "";
		// start += " 00:00:00";
		// end += " 23:59:59";

		String endHour = null;// 带时间点的期初计算
		if (start.length() <= 10) {
			start += " 00:00:00";
		} else {
			endHour = start.substring(10, start.length());
		}
		if (end.length() <= 10) {
			end += " 23:59:59";
		} else {
			endHour = start.substring(10, start.length());
		}

		before_end = start;
		String title1 = ""; // 表头
		String warehouseName = ""; // 仓库名称
		ObjectList warehouseList = ObjectList.getList();
		try {
			DBConnection con = null;
			con = DBConnection.open(false);
			warehouseName = Warehouse.fetchByCode(con, warehouseCode).getName();
			if (warehouseCode.length() == 8) {
				warehouseList = Warehouse.fetchWarehouses(con, warehouseCode);
			}
			con.close();
		} catch (Exception e2) {
			System.out.println(e2.getMessage());
		}
		String startType2 = "";// 按日期查时 起始时间
		String endType2 = "";
		boolean isType2 = false;// 如果是选择日期
		int startDay = Integer.parseInt(start.substring(8, 10));

		GSMonthInfoDao gmi = new GSMonthInfoDao();
		int superMonth = Integer.parseInt(start.substring(5, 7)) - 1;
		int year = Integer.parseInt(start.substring(0, 4));
		if (superMonth == 0) {
			superMonth = 12;
			year = year - 1;
		}
		String dataMonth = "" + year + "-"
				+ (superMonth > 9 ? "" + superMonth : "0" + superMonth);
		String nextMonth = "" + year + "-" + start.substring(5, 7);

		// LogWriter.info("月结开始" + start + "至" + end);
		boolean isAvaliable = true;// 是否保存成功
		boolean isLager = false;// 是否数据过多

		/**
		 * 压库产品进销存 数据提取 开始 (9是新的查询逻辑,不同之处是月结的查询) (10是买断产品进销存) (16是空调报表)
		 * 17是按集团分类进销存报表)
		 */

		if ("1".equals(title)) {
			String masterStart = "";
			String masterEnd = "";
			String masterPart1Start = "";
			String masterPart1End = "";
			String in = "";
			String out = "";
			String newMonthStorage = "";
			Vector vec2 = new Vector();
			String balance_mode = request.getParameter("balanceMode");
			String position = request.getParameter("positionCode");
			if ("1".equals(title)) {
				title1 = warehouseName + "安全库存报表";
			}
			if ("1".equals(title)) {
				/** 按供应商提取出入库明细 */
				int totalSize = 0;

				Vector vec1 = new Vector();
				String all = null;

				DBConnection con = null;
				DBResult dbrs = null;
				ResultSet rs = null;
				// 获取供应商编码
				String psql = "select code from t_provider where name='"
						+ providerName + "' ";
				String pCode = "";
				String tj = "";
				String tj2 = "";
				String balancemodetj ="";
				String balancemodetj2 = "";
				String mydate = getUpDay(-89);
				try {

					con = DBConnection.open(false);
					dbrs = DBEngine.doSelect(con, psql);
					rs = dbrs.getRs();
					while (rs.next()) {
						pCode = rs.getString("code");

					}
					if (pCode != null && !pCode.equals("")) {
						tj = " and b3.provider_code='" + pCode + "' ";
						tj2 = " and a3.provider_code='" + pCode + "' ";
					}
					if(balance_mode.equals("1")){
						balancemodetj="and b3.balance_mode='" + 1 + "' ";
						balancemodetj2="and a3.balance_mode='" + 1 + "' ";
					}else if(balance_mode.equals("2")){
						balancemodetj="and b3.balance_mode='" + 0 + "' ";
						balancemodetj2="and a3.balance_mode='" + 0 + "' ";
					}
					
					all = "select b1.product_code,b1.count1 as cu1,b2.count2 as cu2 from "
							+ "(select a2.product_code,sum(product_count)as count1 from t_out_head a1,t_out_sub a2 "
							+ "where a1.out_bill_code = a2.out_bill_code and a1.status='1' and a2.status='1' "
							+ "and a1.warehouse_code like '"
							+ warehouseCode
							+ "%' "
							+ "and a1.out_time >'"
							+ mydate
							+ "' "
							+ "group by a2.product_code )b1,(select a3.product_code,sum(product_count) as count2 from t_product_storage a3 where a3.status='1' and a3.warehouse_code like '"+warehouseCode+"%' "+tj2+balancemodetj2+" group by a3.product_code )b2,t_product b3 where b1.product_code= b2.product_code and b1.product_code = b3.concentricity_code "
							+ balancemodetj
							+ tj;
					dbrs = DBEngine.doSelect(con, all);
					rs = dbrs.getRs();
					BigDecimal coun1 = null;
					BigDecimal coun2 = null;
					
					String ibs = request.getParameter("ibs");
					while (rs.next()) {
						totalSize++;
						String[] values = new String[3];
						values[0] = rs.getString("product_code");
						//values[1] = rs.getString("cu1");
						if(rs.getString("cu1")!=null&&!rs.getString("cu1").equals("")){
							coun1 = new BigDecimal(rs.getString("cu1"));
							values[1] = coun1.divide(new BigDecimal("3"), 2,BigDecimal.ROUND_HALF_UP).toString();
						}else{
							values[1]="0";
						}
						coun2 = new BigDecimal(rs.getString("cu2"));
						values[2] = coun2.multiply(new BigDecimal(ibs)).toString();
						
						vec1.add(values);
					}

					vec2.add(vec1);
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if (rs != null) {
							rs.close();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
					try {
						if (dbrs != null) {
							dbrs.clear();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					try {
						if (con != null) {
							con.close();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				if (!isAvaliable) {
					isAvaliable = false;
				} else if (totalSize > 18000) {
					isAvaliable = false;
					isLager = true;
				} else {
					if ("1".equals(title)) {
						writeExcel_ReportYKProductAll(month, title, fileName,
								vec2, title1, start, end, warehouseCode,
								request, response,mydate);
					}
				}
			}
		}

		// 封装数据并返回给客户端
		// 判断报表生成的结果
		if (!isAvaliable) {
			if (isLager) {
				request.getSession().setAttribute("MSG", "数据量过于巨大，请缩小查询值的范围");
			} else {
				request.getSession().setAttribute("MSG", "没有查到数据,请重新输入查询条件");
			}
			try {
				String path = "";
				if ("1".equals(title)) {
					path = "/system/report/ReportSafeStorage.jsp";
				}

				getServletConfig().getServletContext()
						.getRequestDispatcher(path).forward(request, response);
			} catch (Exception e) {
				errorMessage(request,response,title,"生成报表出错");
				e.printStackTrace();
			}
		}
		session.setAttribute(IS_REPORT_PROCESSING, Boolean.FALSE);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		doPost(request, response);
	}
	
	private String getUpDay(int num) {
		Date date = new Date();
		date = addDays(date, num);
		return formatByShort(date);
	}
	public static String formatByShort(Date date){
		SimpleDateFormat dateFormat = new SimpleDateFormat(REGEXP_SHORT_DATE);
		return dateFormat.format(date);
	}
	
	public static Date addDays(Date date, int days){
		Calendar calendar = Calendar.getInstance();		
		calendar.setTime(date);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}

	// 文本
	private HSSFCell createCell(HSSFWorkbook aWb, HSSFRow aRow, short column,
			String alaValueue,String[] values) {
		HSSFCellStyle contextStyle = aWb.createCellStyle();
		HSSFDataFormat format = aWb.createDataFormat();
		contextStyle.setDataFormat(format.getFormat("@"));
		HSSFCell tCell = aRow.createCell(column);
		tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		if(values!=null){
			   BigDecimal b1 = new BigDecimal(values[1]);
			if(b1.compareTo(new BigDecimal(values[2]))==1){
				//contextStyle.setFillBackgroundColor(HSSFColor.AQUA.index);
				contextStyle.setFillForegroundColor(HSSFColor.RED.index);//前景颜色
				contextStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			}
			}
		tCell.setCellStyle(contextStyle);
		tCell.setCellValue(alaValueue);
		tCell.setCellType(HSSFCell.CELL_TYPE_STRING);
		
		
		return tCell;
	}

	// 整数
	private HSSFCell createCell2(HSSFWorkbook aWb, HSSFRow aRow, short column,
			String alaValueue,String[] values) {
		HSSFCellStyle contextStyle = aWb.createCellStyle();
		// contextStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0"));
		HSSFCell tCell = aRow.createCell(column);
		tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		tCell.setCellStyle(contextStyle);
		tCell.setCellValue(Double.parseDouble(alaValueue));
		return tCell;
	}

	// 2位小数
	private HSSFCell createCell3(HSSFWorkbook aWb, HSSFRow aRow, short column,
			String alaValueue,String[] values) {
		HSSFCellStyle contextStyle = aWb.createCellStyle();
		contextStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
		HSSFCell tCell = aRow.createCell(column);
		tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		if(values!=null){
			   BigDecimal b1 = new BigDecimal(values[1]);
			if(b1.compareTo(new BigDecimal(values[2]))==1){
				//contextStyle.setFillBackgroundColor(HSSFColor.AQUA.index);
				contextStyle.setFillForegroundColor(HSSFColor.RED.index);//前景颜色
				contextStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			}
			}
		tCell.setCellStyle(contextStyle);
		tCell.setCellValue(Double.parseDouble(alaValueue));
		
		
		return tCell;
	}

	private HSSFCell createCell(HSSFWorkbook aWb, HSSFRow aRow, short column,
			String aValue, short aAlign, boolean border) {
		HSSFCell tCell = aRow.createCell(column);
		tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		tCell.setCellValue(aValue);
		HSSFCellStyle tCellStyle = aWb.createCellStyle();
		tCellStyle.setAlignment(aAlign);
		tCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		if (border) {
			tCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setTopBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setRightBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
		}
		tCell.setCellStyle(tCellStyle);
		return tCell;
	}

	/** 供应商内部类 */
	class ProviderTemp {
		// 供应商编码
		public String providerCode = "";
		// 供应商名称
		public String ProviderName = "";
		public ObjectList listOut = null;
		public ObjectList listIn = null;
		public ObjectList list = null;
	}

	/** 在一个月中 已给出时间 最早start,最终得到 本月的第一天和早于start的最晚时间 */
	private String[] getBeforetime(String start) {
		String[] strs = new String[] { "9999-99-99 99:99:99",
				"0000-00-00 00:00:00" };
		StringBuffer beforeStart = new StringBuffer(start);
		StringBuffer beforeEnd = new StringBuffer(start);
		beforeStart = beforeStart.replace(8, 10, "01");
		int day = Integer.parseInt(String.valueOf(start.subSequence(8, 10)));
		if (day > 1) {
			day--;
		} else {
			return strs;
		}
		String strDay = null;
		if (day < 10) {
			strDay = "0" + day;
		} else {
			strDay = String.valueOf(day);
		}
		beforeEnd = beforeEnd.replace(8, 10, strDay);
		String str1 = String.valueOf(beforeStart);
		String str2 = String.valueOf(beforeEnd);
		strs[0] = str1 + " 00:00:00";
		strs[1] = str2 + " 23:59:59";
		return strs;
	}

	/** 将总集合分割 以product_type为分隔条件 */
	private List splitProductType(List vec) {
		if (vec == null || vec.size() == 0) {
			return null;
		}
		int allSize = vec.size();
		List newVec = new ArrayList();
		List splitVec = new ArrayList();
		boolean needNew = false;
		for (int i = 1; i < allSize; i++) {
			if (needNew) {
				splitVec = new Vector();
				needNew = false;
			}
			String[] values1 = (String[]) vec.get(i);
			String[] values2 = null;
			if (allSize != i + 1) {
				splitVec.add(values1);

			} else {
				splitVec.add(values1);
				newVec.add(splitVec);
			}
		}
		return newVec;
	}

	private boolean isProcessContinue(HttpServletRequest request,
			HttpServletResponse response) {
		String action = request.getParameter("action");

		if (IS_REPORT_PROCESSING.equalsIgnoreCase(action)) {
			String val = request.getParameter("val");
			HttpSession session = request.getSession();
			Boolean isReportProcessing = (Boolean) session
					.getAttribute(IS_REPORT_PROCESSING);
			if (isReportProcessing == null) {
				isReportProcessing = Boolean.FALSE;
			}
			String jsonString = "{result:" + isReportProcessing.booleanValue()
					+ ",val:" + val + "}";
			ResponseUtils.renderJson(response, jsonString);
			return false;
		} else {
			return true;
		} 
	}

	private void errorMessage(HttpServletRequest request,
			HttpServletResponse response, String title, String message)
			throws ServletException {
		try {
			String path = "";
			if ("1".equals(title)) {
				path = "/system/report/ReportSafeStorage.jsp";
			}

			request.getSession().setAttribute("MSG", message);
			getServletConfig().getServletContext().getRequestDispatcher(path)
					.forward(request, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private String mutTimeOrTime(String time1,String time2)
			throws ServletException, ParseException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		time1 = time1.substring(0,4)+time1.substring(5,7)+time1.substring(8,10);
		time2 = time2.substring(0,4)+time2.substring(5,7)+time2.substring(8,10);
		long c = sf.parse(time2).getTime()-sf.parse(time1).getTime();
		long d = c/1000/60/60/24+1;
		return d+"";
	}
	
	

	/** EXCEL 处理 报表 安全库存报表 */
	private void writeExcel_ReportYKProductAll(String month, String title,
			String fileName, Vector vec, String title1, String start,
			String end, String warehouseCode, HttpServletRequest request,
			HttpServletResponse response,String mydate) {
		ServletOutputStream out = null;
		try {
			/** 创建通用EXCEL 开始 */
			HSSFWorkbook tWb = new HSSFWorkbook();
			Region region = null;
			HSSFSheet tSheet = null;
			HSSFRow tRow = null;
			HSSFCell c0 = null;
			HSSFCell c1 = null;
			HSSFCell c2 = null;
			HSSFCell c3 = null;
			
			
			
			HSSFFont font = tWb.createFont();
			font.setFontHeightInPoints((short) 9);
			font.setFontName("宋体");
			font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
			HSSFCellStyle cStyle = tWb.createCellStyle();
			cStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
			HSSFCellStyle cStyleM = tWb.createCellStyle();
			cStyleM.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			cStyleM.setFont(font);
			HSSFCellStyle cStyleM_R = tWb.createCellStyle();
			cStyleM_R.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
			cStyleM_R.setFont(font);
			/** 创建通用EXCEL 结束 */

			/** 创建Sheet 开始 */
			tSheet = tWb.createSheet();

			tWb.setSheetName(0, "安全库存报表", HSSFCell.ENCODING_UTF_16);
			tSheet.setColumnWidth((short) 0, (short) (16 * 256));
			tSheet.setColumnWidth((short) 1, (short) (16 * 256));
			tSheet.setColumnWidth((short) 2, (short) (22 * 256));
			// tSheet.setColumnWidth((short) 11, (short) (14 * 256));

			// 标题行
			tRow = tSheet.createRow((short) 0);
			c0 = createCell(tWb, tRow, (short) 5, title1,null);
			c0.setCellStyle(cStyleM);
			// 日期行
			tRow = tSheet.createRow((short) 1);
			region = new Region(1, (short) 9, 1, (short) 13);
			tSheet.addMergedRegion(region);
			createCell(tWb, tRow, (short) 9, "               " + mydate + " 至 "
					+ getUpDay(0), HSSFCellStyle.ALIGN_CENTER, false);

			// 表头行
			tRow = tSheet.createRow((short) 2);
			// c0 = createCell(tWb, tRow, (short) 11, "");
			c1 = createCell(tWb, tRow, (short) 0, "产品编码",null);
			c2 = createCell(tWb, tRow, (short) 1, "平均出库",null);
			c3 = createCell(tWb, tRow, (short) 2, "当前库存",null);
			

			c1.setCellStyle(cStyleM_R);
			c2.setCellStyle(cStyleM_R);
			c3.setCellStyle(cStyleM_R);
			// c0.setCellStyle(cStyleM_R);
			short index = 2;
			BigDecimal count=null;
			String cou = "";
			if (vec != null && vec.size() > 0) {
				for (int k = 0; k < vec.size(); k++) {
					Vector vec1 = (Vector) vec.get(k);
					if (vec1 == null || !(vec1.size() > 0)) {
						break;
					}
					
					for (int j = 0; j < vec1.size(); j++) {
						String[] values = (String[]) vec1.get(j);
						
						count = new BigDecimal(values[2]);
						cou  = count.divide(new BigDecimal(request.getParameter("ibs")), 2,BigDecimal.ROUND_HALF_UP).toString();
						tRow = tSheet.createRow((short) (index + 1));
						createCell(tWb, tRow, (short) 0,
								values[0] == null ? "" : values[0],values);
						createCell3(tWb, tRow, (short) 1,
								values[1] == null ? "" : values[1],values);
						createCell3(tWb, tRow, (short) 2,
								values[2] == null ? "" : cou,values);
						if(values[1]!=null&&!values[1].equals("")){
						}else{
							values[1]="0";
						}
						if(values[2]!=null&&!values[2].equals("")){	
						}else{
							values[2]="0";
						}
						BigDecimal b1 = new BigDecimal(values[1]);
						index++;

					}
				}
				/** 输出EXCEL文件 开始 */
				fileName = fileName + ".xls";
				response.setContentType("application/ms-excel");
				response.addHeader("Content-disposition", "filename="
						+ fileName);
				out = response.getOutputStream();
				tWb.write(out);
				/** 输出EXCEL文件 结束 */
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {

					e.printStackTrace();
				}
			}
		}
	}
}
